package com.nononsenseapps.feeder.util

import android.content.Context
import android.os.Environment
import timber.log.Timber
import java.io.File


fun ensureDebugLogDeleted(context: Context)  {
    try {
        val logFile = File(context.filesDir, "feeder.log")
        if (logFile.exists()) {
            logFile.delete()
        }
    } catch (exception: Exception) {
        Timber.e("Failed to delete legacy debug log: $exception")
    }
}

fun getAppRootPath(context: Context): File? {
    return if (sdCardIsAvailable()) {
        Environment.getExternalStorageDirectory()
    } else {
        context.filesDir
    }
}

fun sdCardIsAvailable(): Boolean {
    return if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        val sd = File(Environment.getExternalStorageDirectory().getAbsolutePath())
        sd.canWrite()
    } else {
        false
    }
}